package controller.dept;

import controller.BaseServlet;
import dao.DeptDao;
import service.DeptService;
import service.impl.DeptServiceImpl;
import utils.JsonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@WebServlet("/dept/*")
public class DeptServlet extends BaseServlet {

    private DeptService service = new DeptServiceImpl();

    public void findAll(HttpServletRequest req, HttpServletResponse resp){
        try {
            List<Map<String, Object>> all = service.findAll();
            Map<String, String> params = getParams(req);
            if(params.get("flag") != null){
                JsonUtil.writerObjToString(resp,all);
            }else{
                req.setAttribute("deptList",all);
                req.getRequestDispatcher("/view/department").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void findUserByDeptId(HttpServletRequest req, HttpServletResponse resp){
        Map<String, String> param = getParams(req);
        Integer deptId = Integer.valueOf(param.get("id"));
        List<Map<String,Object>> userList=service.findUserByDeptId(deptId);
        JsonUtil.writerObjToString(resp,userList);
    }
}
